Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

342
Visualizações
Why use a generic type and then limit it to one type with "where"?

What's the advantage of using this:

DataTable CopyToDataTable<T>(this IEnumerable<T> source) where T : DataRow

where you can just do this:

DataTable CopyToDataTable<T>(this DataRow[] source)

Excuse me if this is a dumb question, I'm new at this

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I think there are two parts to this.

The first is that accepting an IEnumerable is more flexible than accepting an array. What if the user has a List<DataRow>, or some other collection type? What if they want to pass the result of a linq query? Accepting an array forces them to allocate a new array just to pass to CopyToDataTable, and that's unnecessary cost.

There's no reason why CopyToDataTable needs an array: it just needs an IEnumerable. So it's best if it just accepts an IEnumerable.

That said, due to covariance it would be possible to use the signature:

DataTable CopyToDataTable(this IEnumerable<DataRow> source)

... and users would be able to pass e.g. an IEnumerable<TableDetailsRow> (where TableDetailsRow extends DataRow). However, covariance was only introduced in C# 4, and that method has been around since .NET 3.5, which means it was probably written in C# 3. Since covariance wasn't available then, generics was the next best thing.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda